package com.pzc.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.luojun.entity.User;
import com.pzc.service.UserService;

/**
 * Servlet implementation class DoRegister
 */
@WebServlet("/DoRegister")
public class DoRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoRegister() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		request.setCharacterEncoding("UTF-8");
		String error="";
		String username = request.getParameter("regName");
		String password = request.getParameter("regPwd");
//		String password2 = request.getParameter("password2");
		User u = new User();
		
		
		UserService us = new UserService();
		
//		 u = us.isExist(2);
//		 System.out.println(username);
//		if(us.isExist(username) != null){//判断用户名是否存在
//			error="用户名已存在";
//			request.setAttribute(username, "username");
//			request.setAttribute(error, "error");
//			
//		}else if(!password.equals(password2)){//如果用户名存在，则判断两次输入的密码是否正确
//			error="两次输入的密码不一致";
//			request.setAttribute(error, "error");
//			request.getRequestDispatcher("ToRegister").forward(request, response);
//			
//		}else {
			u.setUserName(username);
			u.setPassword(password);
			int i=us.addUser(u);
			if(i == 1){
				System.out.println("注册成功");
				request.getRequestDispatcher("ToLogin").forward(request, response);
			}else {
				error = "注册失败";
				request.setAttribute(error, "error");
				request.getRequestDispatcher("ToRegister").forward(request, response);
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		}
	}




//}
